package me.zhengjie.modules.bushutongji.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.bushutongji.domain.Goods;
import me.zhengjie.modules.bushutongji.service.GoodsService;
import me.zhengjie.modules.bushutongji.service.dto.GoodsQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

/**
* @author xiexidong 
* @date 2019-08-01
*/
@Api(tags = "Goods管理")
@RestController
@RequestMapping("api")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Log("查询Goods")
    @ApiOperation(value = "查询Goods")
    @GetMapping(value = "/goods")
    @PreAuthorize("hasAnyRole('ADMIN','GOODS_ALL','GOODS_SELECT','kuguan','gongchengshi')")
    public ResponseEntity getGoodss(GoodsQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(goodsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增Goods")
    @ApiOperation(value = "新增Goods")
    @PostMapping(value = "/goods")
    @PreAuthorize("hasAnyRole('ADMIN','GOODS_ALL','GOODS_CREATE','kuguan','gongchengshi')")
    public ResponseEntity create(@Validated @RequestBody Goods resources){
        return new ResponseEntity(goodsService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改Goods")
    @ApiOperation(value = "修改Goods")
    @PutMapping(value = "/goods")
    @PreAuthorize("hasAnyRole('ADMIN','GOODS_ALL','GOODS_EDIT','kuguan','gongchengshi')")
    public ResponseEntity update(@Validated @RequestBody Goods resources){
        goodsService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除Goods")
    @ApiOperation(value = "删除Goods")
    @DeleteMapping(value = "/goods/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','GOODS_ALL','GOODS_DELETE','kuguan','gongchengshi')")
    public ResponseEntity delete(@PathVariable Integer id){
        goodsService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
    @Log("查询Goods")
    @ApiOperation(value = "查询Goods")
    @GetMapping(value = "/getGoods/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','GOODS_ALL','GOODS_DELETE','kuguan','gongchengshi')")
    public ResponseEntity findGoodsByid(@PathVariable Integer id){

        return new ResponseEntity(goodsService.findGoodsById(id),HttpStatus.OK);
    }
}